package main

import (
	"fmt"
	"gitee.com/chunanyong/gouuid"
	"regexp"
	"strconv"
	"strings"
)

//git commit -m "mymodule: changes for v0.1.0"
//git tag v0.1.0
//Push the new tag to the origin repository.
//
//git push origin v0.0.1
//git tag v1.2.3 -m "Release version 1.2.3"

// 删除本地tag
//git tag -d tag-name
// 删除远程tag
// git push origin :refs/tags/v0.0.1

// GetInt 字符串转数字
func GetInt(s *string) int {
	t, _ := strconv.Atoi(*s)
	return t
}

// GetFloat64 字符串转float64
func GetFloat64(s *string) float64 {
	t, _ := strconv.ParseFloat(*s, 64)
	return t

}

// FormatName 料号规则格式化
func FormatName(product string) (name string) {
	//TODO 新问题 待解决，如果匹配不到或中文信息时，应原样返回
	rule1 := regexp.MustCompile(`[0-9]{7,12}`)       //38000038
	rule2 := regexp.MustCompile(`\d{2}-\d{2}-\d{4}`) //39-00-0038
	rule3 := regexp.MustCompile(`\d{3}-\d{4}`)       //870-1039
	rule4 := regexp.MustCompile("-")
	if strings.HasPrefix(product, "0") && rule1.MatchString(product) && len(product) >= 7 {
		product = FormatPrefixZero(&product)
	}

	if strings.HasPrefix(product, "-") && strings.HasPrefix(product[:strings.LastIndex(product, `-`)], "0") {
		product = FormatPrefixZero(&product)
	}

	switch {

	case rule1.MatchString(product) && len(product) >= 7 && !rule4.MatchString(product):

		if len(product) == 7 {

			name = "0" + product[:len(product)-4] + `-` + product[len(product)-4:]
			return name
		} else {

			name = product[:len(product)-4] + `-` + product[len(product)-4:]
			return name
		}

	case rule2.MatchString(product):
		name = strings.Replace(product, "-", "", 1)
		return name

	case rule3.MatchString(product) && len(product[:strings.LastIndex(product, `-`)]) == 3:
		name = `0` + (product)[0:len(product)-4] + (product)[len(product)-4:]
		return name

	default:
		name = ClearSuffix(product)
		return name
	}

}

// ClearSuffix 修改尾缀
func ClearSuffix(s string) (name string) {
	//修改尾椎
	reg := regexp.MustCompile(`\([LFSNVHTAUBKMR]*\).*`)
	name = reg.ReplaceAllString(s, "")
	return name

}

// FormatPrefixZero 清理前缀0
func FormatPrefixZero(s *string) (m string) {

	for {
		if strings.HasPrefix(*s, "0") {
			m = strings.Replace(*s, "0", "", 1)
			s = &m
		} else {
			break
		}
	}
	return m
}

// FormatCleanStringSuffix 清理非法 字段()和空格
func FormatCleanStringSuffix(s string) string {
	var str string
	// 全部转换大写
	str = strings.ToUpper(s)
	// 清理空格
	str = strings.TrimSpace(str)
	// 清理非法横岗
	str = strings.Replace(str, "—", "-", -1)
	// 更正中文括号
	str = strings.Replace(str, "（", "(", -1)
	str = strings.Replace(str, "）", ")", -1)
	fmt.Println(str)
	return str
}

// GetUUID 获取UUID
// type uuid
func GetUUID() (uid gouuid.UUID) { return gouuid.Must(gouuid.NewV4()) }

// GetUUIDString 获取UUID 字符串 无-符号
// type string
func GetUUIDString() string {
	return strings.Replace(gouuid.Must(gouuid.NewV4()).String(), "-", "", -1)
}

// FormatDataCode 格式化DC
func FormatDataCode(str string) (result string) {
	switch len(str) {
	case 1:
		result = ""
	case 4:
		re := regexp.MustCompile(`[0-9]{4}`)
		res := re.MatchString(str)
		if res {
			result = fmt.Sprintf("%s+", str[:2])
		}
	case 5:
		result = fmt.Sprintf("%s+", str[:2])
	case 6:
		result = fmt.Sprintf("%s+", str[:2])
	case 7:
		re := regexp.MustCompile(`[0-9]{4}\.\d`)
		res := re.MatchString(str)
		if res {
			result = fmt.Sprintf("%s+", str[2:4])
			return result
		}
		re2 := regexp.MustCompile(`[0-9]{2}[a-zA-Z]+\d{2}`)
		res2 := re2.MatchString(str)
		if res2 {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
			return result
		} else {
			result = fmt.Sprintf("%s+", str[:2])
			return result
		}
	case 8:
		if strings.Contains(str, ".") {
			return fmt.Sprintf("%s+", str[:2])
		} else {
			if strings.Contains(str, "/") {
				result = fmt.Sprintf("%s+", str[len(str)-2:])
			} else if strings.Contains(str, "-") {
				result = fmt.Sprintf("%s+", str[:2])
				return result
			} else {
				result = fmt.Sprintf("%s+", str[2:4])

			}
		}
	case 9:
		re := regexp.MustCompile(`[a-zA-Z]+`)
		res := re.MatchString(str)
		if res {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
			return result
		}

		re1 := regexp.MustCompile(`\d/\d{2}/\d{4}`)
		res1 := re1.MatchString(str)
		if res1 {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
			return result
		}

		re2 := regexp.MustCompile(`\d{4}\.\d{2}\.\d{2}`)
		res2 := re2.MatchString(str)
		if res2 {
			result = fmt.Sprintf("%s+", str[2:4])
			return result
		}
		re3 := regexp.MustCompile(`\d{2}[a-zA-Z]+\d{2}`)
		res3 := re3.MatchString(str)
		if res3 {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
			return result
		}
		re4 := regexp.MustCompile(`\d{2}/\d/\d{4}`)
		res4 := re4.MatchString(str)
		if res4 {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
			return result
		}
		re5 := regexp.MustCompile(`\d{2}\.[a-zA-Z]+\.\d{2}`)
		res5 := re5.MatchString(str)
		if res5 {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
			return result
		}
		re6 := regexp.MustCompile(`\d{2}[a-zA-Z]+\d{4}`)
		res6 := re6.MatchString(str)
		if res6 {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
			return result
		} else {
			result = fmt.Sprintf("%s+", str[2:4])
		}

	case 10:
		re := regexp.MustCompile(`\d{4}/\d{2}/\d{2}`)
		res := re.MatchString(str)
		if res {
			result = fmt.Sprintf("%s+", str[2:4])
		}
		re1 := regexp.MustCompile(`\d{2}/\d{2}/\d{4}`)
		res1 := re1.MatchString(str)
		if res1 {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
		}
		re2 := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
		res2 := re2.MatchString(str)
		if res2 {
			result = fmt.Sprintf("%s+", str[2:4])
		}
	case 11:
		re := regexp.MustCompile(`\d{2}\.[A-Za-z]+\.\d{4}`)
		res := re.MatchString(str)
		if res {
			result = fmt.Sprintf("%s+", str[len(str)-2:])
		} else {
			result = fmt.Sprintf("%s+", str[2:4])
		}

	default:
		result = str
	}
	return result
}

// FormatYAZAKI YAZAKI 规则
func FormatYAZAKI(val *string) string {
	//71296030 7129-603000-3W
	// '^7[0-3HCWTE]{1}[12358]{1}[234678]{1}-[0-9PY]{4,}$|^7[0-3HCWTE]{1}[12358]{1}[234678]{1}-[0-9PY]{4,}-[0-9]{1,2}$'
	if len(*val) <= 9 {
		return fmt.Sprintf("%s-%s", (*val)[:4], (*val)[4:])
	} else {
		return fmt.Sprintf("%s-%s-%s", (*val)[:4], (*val)[4:8], (*val)[8:])
	}
}
